﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.Utility;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Consts;
using Sector7.Framework.DataDictionary.Agents;

namespace Sector7.Framework.DataDictionary.Windows
{
    /// <summary>
    /// AddDataTemplateWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddDataTemplateWindow : AddModeWindowBase
    {
        public AddDataTemplateWindow()
        {
            InitializeComponent();
        }

        public S7_DataTemplate DataTemplate
        {
            get { return m_entity as S7_DataTemplate; }
            set { m_entity = value; }
        }

        protected override void InitializeForCreating()
        {
            cbbGroup.SelectedItem = null;
            txbTemplateName.Text = string.Empty;
            txbTableName.Text = string.Empty;

            m_entity = new S7_DataTemplate();
            DataTemplate.Items = new List<S7_DataTemplateItem>();

            ClientUtil.LoadItemsSource(dtgItems, DataTemplate.Items);
        }

        protected override void InitializeForUpdating()
        {
            btnContinue.Visibility = Visibility.Hidden;

            ClientUtil.LoadItemsSource(dtgItems, DataTemplate.Items);
        }

        protected override void AddData()
        {
            DataTemplate.GroupId = Convert.ToInt32(cbbGroup.SelectedValue);
            DataTemplate.TemplateName = txbTemplateName.Text;
            DataTemplate.TableName = txbTableName.Text;

            base.AddData();
        }

        private void GetData()
        {
            ClientUtil.LoadItemsSource(cbbGroup, DataGroupServiceAgent.GetAll(C_DataGroupType.DATA_TEMPLATE));
        }

        protected override void DoOpen()
        {
            base.DoOpen();

            var itemSummaryWindow = new DataTemplateItemSummaryWindow();
            S7_DataTemplate DataTemp = new S7_DataTemplate();
            DataTemp.GroupId = Convert.ToInt32(cbbGroup.SelectedValue);
            itemSummaryWindow.DataTemplates = DataTemp;
            itemSummaryWindow.InitializeWindow(CrudType.Create);
            itemSummaryWindow.AddSuccess += new EventHandler(itemSummaryWindow_AddSuccess);

            itemSummaryWindow.ShowDialog();
        }

        void itemSummaryWindow_AddSuccess(object sender, EventArgs e)
        {
            var itemSummaryWindow = sender as DataTemplateItemSummaryWindow;
            DataTemplate.Items.Clear();
            DataTemplate.Items.AddRange(itemSummaryWindow.SelectedItems);

            ClientUtil.LoadItemsSource(dtgItems, DataTemplate.Items);
        }

        private void dtgItems_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {

            }
        }

        private void dtgItems_SelectionChanged(object obj, SelectionChangedEventArgs e)
        {
            var selectedDataTemplateItem = dtgItems.SelectedItem as S7_DataTemplateItem;

            if (selectedDataTemplateItem != null)
            {
                dtgItems.ContextMenu = CreateTypeMenu(selectedDataTemplateItem.Metadata.ColumnName);
            }
        }

        private ContextMenu CreateTypeMenu(string columnName)
        {
            var menu = new ContextMenu();

            var menuPKItem = new MenuItem();
            menuPKItem.Header = "设置主键";
            menuPKItem.Tag = CrudType.Create;
            menuPKItem.Click += new RoutedEventHandler(PK_Menu_Click);

            var menuFKItem = new MenuItem();
            menuFKItem.Header = "设置外键";
            menuFKItem.Tag = CrudType.Create;
            menuFKItem.Click += new RoutedEventHandler(FK_Menu_Click);

            menu.Items.Add(menuPKItem);
            menu.Items.Add(menuFKItem);
            return menu;
        }

        /// <summary>
        /// 设置主键
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void PK_Menu_Click(object obj, RoutedEventArgs e)
        {
            var addPrimaryKeyWindow = new AddPrimaryKeyWindow();
            var selectedDataTemplateItem = dtgItems.SelectedItem as S7_DataTemplateItem;
            addPrimaryKeyWindow.ColumnName = selectedDataTemplateItem.Metadata.ColumnName;
            addPrimaryKeyWindow.InitializeWindow(CrudType.Update);
            addPrimaryKeyWindow.AddSuccess += new EventHandler(addPrimaryKeyWindow_AddSuccess);
            addPrimaryKeyWindow.ShowDialog();
        }

        /// <summary>
        /// 设置外键
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="e"></param>
        private void addPrimaryKeyWindow_AddSuccess(object obj, EventArgs e)
        {
            var selectedDataTemplateItem = dtgItems.SelectedItem as S7_DataTemplateItem;
            AddPrimaryKeyWindow addWindow = obj as AddPrimaryKeyWindow;
            selectedDataTemplateItem.Metadata.isAuto = addWindow.Metadata.isAuto;
            selectedDataTemplateItem.Metadata.KeyType = addWindow.Metadata.KeyType;
        }

        private void FK_Menu_Click(object obj, RoutedEventArgs e)
        {
            var addForeignKeyWindow = new AddForeignKeyWindow();
            //addForeignKeyWindow.AddSuccess += new EventHandler(addForeignKeyWindow_AddSuccess);
            //addForeignKeyWindow.ShowDialog();
        }

        private void addForeignKeyWindow_AddSuccess(object obj, EventArgs e)
        {
            //var selectedItem = dtgMain.SelectedItem as S7_DataTemplate;
            //DataTemplateServiceAgent.SetTableKey(selectedItem.TableName, selectedItem.TemplateId.ToString());
        }

        private void AddModeWindowBase_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
        }

        private void btnAddItem_Click(object sender, RoutedEventArgs e)
        {
            DoOpen();
        }
    }
}
